package com.huluobo.high3demo.setting;

import androidx.lifecycle.MutableLiveData;

import com.huluobo.mvvm.baen.CommentBeanResult;
import com.huluobo.mvvm.baen.SettingBean;
import com.huluobo.mvvm.model.BaseRepo;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MultipartBody;

/**
 * Created by LC on 2023/10/6.
 */
public class SettingRepo extends BaseRepo {

    /**
     * 上传头像
     *
     * @param part    需要一个part,类似json的转换
     * @param success 成功的回调
     * @param failed  失败的回调
     */
    public void updateUserIcon(MultipartBody.Part part, MutableLiveData<SettingBean> success,
                               MutableLiveData<String> failed) {
        apiService.updateIcon(part)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SettingBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(SettingBean settingBean) {
                        success.setValue(settingBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void updateAddress(String address, MutableLiveData<SettingBean> success, MutableLiveData<String> failed) {
        apiService.updateAddress(address)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SettingBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(SettingBean settingBean) {
                        success.setValue(settingBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void updateUserNickname(String nickname, MutableLiveData<SettingBean> success,
                                   MutableLiveData<String> failed) {
        apiService.updateNickname(nickname)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SettingBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(SettingBean settingBean) {
                        success.setValue(settingBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void updateBrith(String brith, MutableLiveData<SettingBean> success, MutableLiveData<String> failed) {
        apiService.updateBrith(brith).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SettingBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(SettingBean settingBean) {
                        success.setValue(settingBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void updateSex(String sex, MutableLiveData<SettingBean> success, MutableLiveData<String> failed) {
        apiService.updateSex(sex)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SettingBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(SettingBean settingBean) {
                        success.setValue(settingBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
